package cn.bytecollege.controller;

import cn.bytecollege.constant.Resource;
import cn.bytecollege.entity.Room;
import cn.bytecollege.entity.RoomCategory;
import cn.bytecollege.service.RoomCategoryService;
import cn.bytecollege.service.RoomService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * YL
 * 房间查找
 * RoomFindController
 */
@WebServlet(name = "RoomFindController",value = "/roomFind")
public class RoomFindController extends HttpServlet {
    String msg=null;
    private RoomService roomService = null;
    private RoomCategoryService roomCategoryService = null;
    public RoomFindController(){
        roomService = new RoomService();
        roomCategoryService = new RoomCategoryService();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        Integer roomId = Integer.valueOf(id);
        Room room = roomService.findById(roomId);
        if (room.getRoomState()==1){
            msg="该房间已入住，不可修改";
            request.setAttribute("msg", msg);
            request.getRequestDispatcher("/roomListAll").forward(request,response);
            return;
        }
        request.setAttribute("room",room);
        List<RoomCategory> list = roomCategoryService.find();
        request.setAttribute("list",list);
        request.getRequestDispatcher(Resource.PAGE_LOCATION + "roomUpdate.jsp")
                .forward(request,response);

    }
}
